package com.LeetCode.dp.String;


import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * 划分字符串
 */
public class SplitString {

    public boolean wordBreak(String s, List<String> wordDict) {
        HashSet<String> set = new HashSet<>();
        int maxLen = 0;
        int len = s.length();
        boolean[] dp = new boolean[len + 1];
        dp[0] = true;
        for(String word : wordDict){
            set.add(word);
            maxLen = Math.max(maxLen, word.length());
        }

        for(int i = 1; i<len+1; i++){
            for(int j = i; j>=0 && j >= i - maxLen; j--){
                if(dp[j] && set.contains(s.substring(j, i))){
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[len];
    }

    @Test
    public void test(){
        String s = "leetcode";
        List<String> dict = new ArrayList<>(Arrays.asList("leet", "code"));
        System.out.println(wordBreak(s,dict));
    }
}
